import { request } from "@/utils/request";
import type {  VideoItem, VideoListResult  } from "@/types/dailyDharma";

/**
 * 获取视频列表
 * @param keyword 搜索关键词
 * @param pageNum 页码，默认为1
 * @param pageSize 每页数量，默认为10
 * @returns 视频列表查询结果
 */
export const getVideoList = async (
  keyword: string = "",
  pageNum: number = 1,
  pageSize: number = 10
): Promise<VideoListResult> => {
  try {
    const params = new URLSearchParams();
    if (keyword) {
      params.append("keyword", keyword);
    }
    params.append("pageNum", pageNum.toString());
    params.append("pageSize", pageSize.toString());

    const url = `/scripture/video/list?${params.toString()}`;
    console.log("获取视频列表URL:", url);

    // 使用request.get方法而不是直接调用request
    const response = await request.get<VideoListResult>(url);
    
    // 检查响应状态
    if (response.code === 200) {
      // 将ResponseData转换为VideoListResult
      console.log("response3333333333:", response);
      return response as any;
    } else {
      throw new Error(response.message || "获取视频列表失败");
    }
  } catch (error) {
    console.error("获取视频列表失败:", error);
    throw error;
  }
};

/**
 * 搜索视频
 * @param keyword 搜索关键词
 * @returns 搜索结果
 */
export const searchVideos = async (
  keyword: string
): Promise<VideoItem[]> => {
  try {
    if (!keyword.trim()) {
      return [];
    }
    
    const result = await getVideoList(keyword);
    return result.rows || [];
  } catch (error) {
    console.error("搜索视频失败:", error);
    throw error;
  }
};

/**
 * 获取视频详情
 * @param id 视频ID
 * @returns 视频详情
 */
export const getVideoDetail = async (id: number): Promise<VideoItem> => {
  try {
    const url = `/scripture/video/${id}`;
    console.log("获取视频详情URL:", url);
    
    // 使用request.get方法而不是直接调用request
    const res = await request.get<VideoItem>(url);

    if (res.code === 200 && res.data) {
      return res.data;
    } else {
      throw new Error(res.message || "获取视频详情失败");
    }
  } catch (error) {
    console.error("获取视频详情失败:", error);
    throw error;
  }
};
